var t = require("../../../../@babel/runtime/helpers/interopRequireDefault"), a = t(require("../../../../dist/dialog/dialog")), i = t(require("../../../../utils/polyvlive.js")), s = require("../../../../utils/CryptoJS.js"), o = require("../../../../mycomponent/yz_pay/yz_pay"), n = getApp();

Page({
    behaviors: [ o ],
    data: {
        is_try_time: null,
        is_questionnaire_time: null,
        questionnaire_is_open: !1,
        language: "",
        goods_id: "",
        is_show: !0,
        is_vshow: !1,
        isVideo: !0,
        isLook: !0,
        is_update: !1,
        is_buy: !1,
        showPopup: !1,
        rewardMoneyBtn: !1,
        actionSheetShow: !1,
        rewardBtnShow: !1,
        rewardMoney: "",
        goodTit: "",
        goodPrice: "",
        chapterNum: "",
        teacherImg: "",
        teacherName: "",
        goodsContent: "",
        chapterList: [],
        article_content: "",
        courseShare: {},
        courseData: {},
        codeUrl: "",
        vedioSrc: "",
        coverImg: "",
        active: "",
        chapter_id: "",
        courseId: "",
        pushGoodList: [],
        actionSheetItems: [],
        isLoding: !1,
        activeNames: [ "1" ],
        chapterdigList: [],
        vip_level_status: {},
        islive: !1,
        video_err: !1,
        is_playLive: !1,
        live: !1,
        videoErr: !0,
        ios_virtual_pay: !1,
        courseTabsActive: 1,
        posterShow: !1
    },
    onLoad: function(t) {
        console.log(t), this.setData({
            goods_id: t.goods_id,
            questionnaire_is_open: !1
        }), this._getData();
        try {
            var a = wx.getStorageSync("ios_virtual_pay");
            a && this.setData({
                ios_virtual_pay: a
            });
        } catch (t) {
            console.log(t);
        }
        try {
            var i = wx.getStorageSync("yz_basic_info");
            this.setData({
                shopName: i.home.mailInfo.name
            });
        } catch (t) {
            console.log(e);
        }
    },
    changeLoading: function() {
        this.setData({
            isLoding: !0
        });
    },
    videoErr: function() {
        wx.showToast({
            title: "视频加载出错了！",
            icon: "none",
            duration: 2e3
        }), this.setData({
            videoErr: !1
        });
    },
    setPosterShow: function() {
        this.setData({
            posterShow: !this.data.posterShow
        });
    },
    getQrCode: function() {
        var t = this;
        n._getNetWork({
            url: n.getNetAddresss("member.member.getArticleQr"),
            success: function(e) {
                if (1 != e.data.result || n._isTextEmpty(e.data.data)) console.error(e); else {
                    var a = e.data.data;
                    t.setData({
                        codeUrl: a
                    });
                }
            },
            fail: function(t) {
                console.error(t);
            }
        });
    },
    secret: function(t, e, a) {
        e = s.MD5(e).toString();
        var i = s.enc.Utf8.parse(e.substring(0, 16)), o = s.enc.Utf8.parse(e.substring(16));
        return a ? s.AES.decrypt(t, o, {
            iv: i,
            padding: s.pad.Pkcs7
        }).toString(s.enc.Utf8) : s.AES.encrypt(t, o, {
            iv: i,
            mode: s.mode.CBC,
            padding: s.pad.Pkcs7
        }).toString();
    },
    addzhibo: function() {
        var t = this;
        n._getNetWork({
            url: n.getNetAddresss("plugin.video-demand.api.video-course-goods.get-live-info"),
            data: {
                goods_id: t.data.goods_id
            },
            success: function(e) {
                if (console.log(e), 1 == e.data.result) {
                    if (console.log(e.data.data), 1 == t.data.islive) return void wx.showToast({
                        title: "正在直播",
                        icon: "none",
                        duration: 1e3
                    });
                    var a = JSON.parse(t.secret(e.data.data, "zoQUA@257ggflNmMppKzcU7QgYyp!tU&h54VNN3u!PAU$8FxBW", !0));
                    console.log(a), t.is_onlive(a.userId, a.channelId), t.setData({
                        islive: !0
                    });
                } else wx.showToast({
                    title: e.data.msg,
                    icon: "none",
                    duration: 1e3
                }), t.setData({
                    is_update: !0,
                    is_buy: !0
                });
            },
            fail: function(t) {
                console.error(t);
            }
        });
    },
    is_onlive: function(t, e) {
        var a = this;
        wx.showLoading({
            title: "加载直播中",
            icon: "none"
        }), console.log();
        var s = wx.createLivePlayerContext("polyvLiveVideo");
        this.setData({
            islive: !0
        }), i.default.getVideo({
            uid: t,
            vid: e,
            videoContext: s,
            success: function(t) {
                console.log(t), a.setPlayerSrc(t);
            },
            error: function(t) {
                console.log(t.error);
            },
            onStartLive: function() {
                console.log("start live"), wx.hideLoading(), a.setNewPlayerSrc();
            },
            onApiStatus: function(t) {
                console.log(t), "end" == t ? (a.setData({
                    is_playLive: !1,
                    islive: !1
                }), wx.hideLoading(), wx.showToast({
                    title: "当前无直播",
                    icon: "none",
                    duration: 1e3
                }), i.default.destroy()) : "live" == t && a.setData({
                    is_playLive: !0
                });
            }
        });
    },
    setPlayerSrc: function(t) {
        this.setData({
            video: {
                src: t.flvSrc,
                poster: t.poster
            },
            is_bisrc: t.src
        }), console.log(t.src);
    },
    setNewPlayerSrc: function() {
        var t = this;
        i.default.getNewVideo(function(e) {
            t.setPlayerSrc(e);
        });
    },
    onShareTimeline: function() {
        var t = wx.getStorageSync("yz_uid"), e = "";
        return t && (e = t), {
            title: n._isTextEmpty(this.data.courseShare.share_title) ? this.data.goodTit : this.data.courseShare.share_title,
            query: "share_tag=2&goods_id=".concat(this.data.goods_id, "&mid=").concat(e),
            imageUrl: n._isTextEmpty(this.data.courseShare.share_thumb) && this.data.courseData.has_one_goods ? this.data.courseData.has_one_goods.thumb : this.data.courseShare.share_thumb
        };
    },
    onShareAppMessage: function(t) {
        var e = wx.getStorageSync("yz_uid"), a = "";
        e && (a = e);
        var i = n._isTextEmpty(this.data.courseShare.share_title) ? this.data.goodTit : this.data.courseShare.share_title, s = "/packageA/detail_v2/detail_v2?id=" + this.data.goods_id + "&mid=" + a, o = n._isTextEmpty(this.data.courseShare.share_thumb) && this.data.courseData.has_one_goods ? this.data.courseData.has_one_goods.thumb : this.data.courseShare.share_thumb;
        return {
            title: i,
            desc: this.data.courseShare.share_desc,
            imageUrl: o,
            path: s,
            success: function(t) {
                console.info(t + "成功");
            },
            fail: function(t) {
                console.log(t + "失败");
            },
            complete: function(t) {}
        };
    },
    _getData: function() {
        var t = this;
        n._getNetWork({
            url: n.getNetAddresss("plugin.video-demand.api.video-course-goods.get-course-goods-detail"),
            data: {
                goods_id: t.data.goods_id
            },
            success: function(e) {
                if (1 === e.data.result) {
                    var i = e.data.data;
                    if (n._isTextEmpty(i.has_many_dig_chapter) || 0 == i.has_many_dig_chapter.length) return void a.default.alert({
                        title: "提示",
                        message: "课程已下架！",
                        context: t
                    }).then(function() {
                        wx.navigateBack({
                            delta: 1
                        });
                    });
                    t.setData({
                        chapterdigList: i.has_many_dig_chapter
                    }), i.has_one_goods.content = i.has_one_goods.content.replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block" ');
                    for (var s = "", o = 0, r = i.has_many_dig_chapter, d = 0; d < r.length; d++) if (r[d].has_many_chapter.length > 0 && t.data.chapter_id) for (var c = 0; c < r[d].has_many_chapter.length; c++) r[d].has_many_chapter[c].id === Number(t.data.chapter_id) && (s = r[d].has_many_chapter[c], 
                    o = c); else s = r[d].has_many_chapter[0];
                    t.setData({
                        courseData: i,
                        rewardBtnShow: i.is_reward,
                        goodTit: i.has_one_goods.title,
                        goodPrice: i.has_one_goods.price,
                        vip_level_status: i.has_one_goods.vip_level_status,
                        chapterNum: i.course_chapter_num,
                        teacherImg: i.has_one_lecturer && i.has_one_lecturer.has_one_member ? i.has_one_lecturer.has_one_member.avatar : "",
                        teacherName: i.has_one_lecturer && i.has_one_lecturer.real_name,
                        goodsContent: i.has_one_goods.content,
                        chapter_id: s.id,
                        isLook: s.is_audition,
                        courseId: i.id,
                        pushGoodList: i.pushGoods,
                        courseShare: i.has_one_goods.has_one_share,
                        live: i.live,
                        live_title: i.live_title,
                        questionnaire_time: i.questionnaire_time,
                        plugin_point_name: i.plugin_point_name ? i.plugin_point_name : "积分"
                    });
                    var u = 1e3 * i.questionnaire_time;
                    switch (i.questionnaire_is_open && (this.data.is_questionnaire_time = setTimeout(function() {
                        t.data.is_questionnaire_time && t.setData({
                            questionnaire_is_open: i.questionnaire_is_open
                        });
                    }, u)), i.watch) {
                      case 0:
                        t.setData({
                            is_update: !1,
                            is_buy: !1
                        }), t.videoInit(o, s);
                        break;

                      case 1:
                        if (1 == s.is_audition) {
                            t.setData({
                                is_update: !1,
                                is_buy: !1,
                                is_show: !1,
                                is_vshow: !0
                            }), t.videoInit(o, s);
                            break;
                        }
                        if (0 == s.is_audition) {
                            a.default.alert({
                                title: "提示",
                                message: "请升级会员或者购买课程",
                                context: t
                            }).then(function() {}), t.setData({
                                is_update: !0,
                                is_buy: !0
                            });
                            break;
                        }
                        break;

                      case 2:
                      case 3:
                        t.setData({
                            is_update: !1,
                            is_buy: !1
                        }), t.videoInit(o, s);
                    }
                } else wx.showToast({
                    title: "视频加载出错",
                    icon: "none",
                    duration: 1500
                }), console.error(e);
            },
            fail: function(t) {
                console.error(t);
            }
        });
    },
    videoInit: function(t, e) {
        this.setData({
            is_show: !1,
            is_vshow: !0,
            active: e.id
        }), this.viewPermission(e.id, t, e.course_id, e.is_audition);
    },
    viewPermissionBtn: function(t) {
        var e = t.currentTarget.dataset, a = e.item.id, i = e.index, s = e.item.course_id, o = e.item.is_audition;
        console.log(a, i, s, o), console.log(e), this.viewPermission(a, i, s, o);
    },
    viewPermission: function(t, e, a, i) {
        var s = this;
        this.setData({
            is_show: !0,
            is_vshow: !1,
            islive: !1
        }), this.setData({
            chapter_id: t,
            active: t
        });
        var o = {
            goods_id: this.data.goods_id,
            chapter_id: t
        }, r = n.getNetAddresss("plugin.video-demand.api.video-course-goods.get-video-address");
        n._getNetWork({
            url: r,
            data: o,
            success: function(o) {
                if (1 == o.data.result) {
                    var n = o.data.data;
                    s.viewType(n.watch, e, n.video_address, a, t, i, n.video_cover_address, n.is_try, n.try_time);
                } else console.error(o);
            },
            fail: function(t) {
                console.error(t);
            }
        });
    },
    viewType: function(t, e, i, s, o, n, r, d, c) {
        var u = this;
        if (1 == d && 1 == t && 1 == n) return this.setData({
            is_update: !1,
            is_buy: !1,
            isLook: !0,
            is_vshow: !0,
            is_show: !1
        }), void this.videoPlay(e, i, s, o, r);
        if (2 != t && 3 != t && 1 == d && c > 0) return this.setData({
            is_update: !1,
            is_buy: !1,
            isLook: !0
        }), this.videoPlay(e, i, s, o, r), void (this.data.is_try_time || (this.data.is_try_time = setTimeout(function() {
            u.setData({
                islive: !1,
                is_show: !0,
                is_vshow: !1
            }), u.data.is_try_time && wx.showModal({
                title: "提示",
                content: "请升级会员或者购买课程"
            }), u.setData({
                is_update: !0,
                is_buy: !0,
                isLook: !1,
                is_vshow: !1,
                is_show: !0
            });
        }, 1e3 * c)));
        switch (t) {
          case 0:
            wx.showModal({
                title: "提示",
                content: "请升级会员或者购买课程"
            }), this.setData({
                is_update: !0,
                is_buy: !0,
                isLook: !1,
                is_vshow: !1,
                is_show: !0
            });
            break;

          case 1:
            if (1 == n) {
                this.setData({
                    is_update: !1,
                    is_buy: !1,
                    isLook: !0,
                    is_vshow: !0,
                    is_show: !1
                }), this.videoPlay(e, i, s, o, r);
                break;
            }
            if (0 == n) {
                a.default.alert({
                    title: "提示",
                    message: "请升级会员或者购买课程",
                    context: that
                }).then(function() {}), this.setData({
                    is_update: !0,
                    is_buy: !0,
                    isLook: !1
                });
                break;
            }
            break;

          case 2:
          case 3:
            this.setData({
                is_update: !1,
                is_buy: !1,
                isLook: !0
            }), this.videoPlay(e, i, s, o, r);
        }
    },
    videoPlay: function(t, e, a, i, s) {
        console.log(t, e, a, i, s), this.setData({
            is_show: !1,
            is_vshow: !0,
            active: i,
            vedioSrc: e,
            coverImg: s
        }), this.isMp4(this.data.vedioSrc), n._getNetWork({
            url: n.getNetAddresss("plugin.video-demand.api.video-demand-member.set-watch-history"),
            data: {
                course_id: a,
                chapter_id: i
            },
            success: function(t) {
                t.data.result;
            },
            fail: function(t) {
                console.error(t);
            }
        });
    },
    isMp4: function(t) {
        t.endsWith(".mp4") ? this.setData({
            isVideo: !0,
            is_show: !1
        }) : this.setData({
            isVideo: !1,
            is_show: !1
        });
    },
    study: function() {
        this.setData({
            is_show: !1,
            is_vshow: !0
        });
    },
    update: function() {
        if (this.data.ios_virtual_pay) return wx.showModal({
            title: "提示",
            content: "十分抱歉，由于相关规定，你暂时无法在这里充值！",
            confirmText: "知道了",
            showCancel: !1,
            success: function(t) {
                t.confirm && console.log(t);
            }
        }), !1;
        wx.navigateTo({
            url: "/packageA/member/course/CourseMemberUpdate/CourseMemberUpdate?goods_id=" + this.data.goods_id
        });
    },
    buy: function() {
        return this.data.ios_virtual_pay ? (wx.showModal({
            title: "提示",
            content: "十分抱歉，由于相关规定，你暂时无法在这里充值！",
            confirmText: "知道了",
            showCancel: !1,
            success: function(t) {
                t.confirm && console.log(t);
            }
        }), !1) : this.data.vip_level_status && 1 == this.data.vip_level_status.status ? (wx.showToast({
            title: this.data.vip_level_status.tips,
            icon: "none",
            duration: 1500
        }), !1) : void (this.data.loadBtn || wx.navigateTo({
            url: "/packageD/buy/myOrder_v2/myOrder_v2?tag=-2&total=1&optionsId=&goodsId=" + this.data.goods_id
        }));
    },
    toMyCourse: function() {
        wx.navigateTo({
            url: "/packageA/member/course/CourseMy/CourseMy"
        });
    },
    pushGoodGoto: function(t) {
        var e = t.currentTarget.dataset.item;
        wx.navigateTo({
            url: "/packageA/detail_v2/detail_v2?id=" + e.id
        });
    },
    chooseMoney: function(t) {
        var e = t.currentTarget.dataset.money;
        this.setData({
            rewardMoney: e
        });
    },
    confirmReward: function() {
        n._isTextEmpty(this.data.rewardMoney) ? wx.showToast({
            title: "请输入或选择打赏金额",
            icon: "none",
            duration: 1500
        }) : this.data.rewardMoney <= 0 ? wx.showToast({
            title: "打赏金额必须大于0",
            icon: "none",
            duration: 1500
        }) : this.getPayData();
    },
    getPayData: function() {
        var t = this;
        n._getNetWork({
            url: n.getNetAddresss("plugin.video-demand.api.lecturer-reward.pay-type"),
            success: function(e) {
                if (1 === e.data.result) {
                    var a = e.data.data.buttons;
                    t.setData({
                        showPopup: !1,
                        btnData: a,
                        rewardPayshow: !0
                    });
                } else console.error(e);
            },
            fail: function(t) {
                console.error(t);
            }
        });
    },
    rewardPayshowClose: function() {
        this.setData({
            rewardPayshow: !1
        });
    },
    rewardPayCancelBtn: function() {
        this.setData({
            rewardPayshow: !1
        });
    },
    payPost: function(t) {
        var e = this;
        this.setData({
            rewardPayshow: !1
        });
        var i, s, o = null;
        t.currentTarget ? (o = t.currentTarget.dataset.info, i = t.currentTarget.dataset.info.value, 
        s = t.currentTarget.dataset.info.name) : (o = t, i = t.value, s = t.name);
        var r = {
            pay_method: i,
            amount: this.data.rewardMoney,
            pay_name: s,
            trick_id: this.data.stickid,
            client_type: "2",
            goods_id: this.data.goods_id
        };
        n._getNetWork({
            url: n.getNetAddresss("plugin.video-demand.api.lecturer-reward.run-reward-pay"),
            data: r,
            success: function(t) {
                1 === t.data.result ? (e.handleOtherPay(o, i, t.data.data), e.onClose()) : (a.default.alert({
                    title: "提示",
                    message: t.data.msg
                }).then(function() {}), console.error(t));
            },
            fail: function(t) {
                console.error(t);
            }
        });
    },
    moneyShow: function() {
        this.setData({
            showPopup: !0
        });
    },
    openInputReward: function() {
        this.setData({
            showPopup: !1,
            rewardMoneyBtn: !0
        });
    },
    onClose: function() {
        this.setData({
            showPopup: !1,
            rewardMoneyBtn: !1,
            actionSheetShow: !1,
            questionnaire_is_open: !1
        });
    },
    goSurvey: function() {
        wx.navigateTo({
            url: "/packageE/questionnaire/questions/questions?goods_id=" + this.data.goods_id + "&tag=video"
        });
    },
    openActionSheet: function() {
        this.setData({
            showPopup: !1,
            actionSheetShow: !0
        });
    },
    previewImage: function(t) {
        var e = t.currentTarget.dataset.src;
        wx.previewImage({
            current: e,
            urls: [ e ]
        });
    },
    onChange: function(t) {
        this.setData({
            activeNames: t.detail
        });
    },
    onReady: function() {
        var t = wx.getStorageSync("langIndex");
        this.setData({
            language: t.en
        }), wx.setNavigationBarTitle({
            title: "课程详情"
        });
    },
    onShow: function() {
        wx.showShareMenu({
            withShareTicket: !1,
            menus: [ "shareAppMessage", "shareTimeline" ]
        }), this._getData();
    },
    onHide: function() {
        this.data.is_try_time = null, this.data.is_questionnaire_time = null, clearTimeout(this.data.is_try_time), 
        clearTimeout(this.data.is_questionnaire_time), console.log("onHide监听小程序隐藏", this.data.is_try_time);
    },
    onUnload: function() {
        this.data.is_try_time = null, this.data.is_questionnaire_time = null, clearTimeout(this.data.is_try_time), 
        clearTimeout(this.data.is_questionnaire_time), console.log("onUnload监听小程序隐藏", this.data.is_try_time);
    }
});